package com.grom.dao;

import java.util.List;

import com.grom.po.Security;
import com.grom.util.Constants;
import com.grom.util.IDCreator;
import com.holder.DBContextHolder;
import com.holder.DBUTil;

class SecurityDaoImpl implements SecurityDao {
	public List<Security> getAllSecuritys() {
		DBContextHolder.setContextConnection(DatabaseDao.getConnection());
		return DBUTil.select(Security.class);
	}

	public void add(Security security) {
		if(security.getID()==null){
			security.setID(IDCreator.getSingleId());
		}
		DBContextHolder.setContextConnection(DatabaseDao.getConnection());
		DBUTil.save(security);
	}

	public void update(Security security) {
		DBContextHolder.setContextConnection(DatabaseDao.getConnection());
		DBUTil.update(security);
	}

	public void delete(Security security) {
		DBContextHolder.setContextConnection(DatabaseDao.getConnection());
		DBUTil.delete(security);
	}

	public List<Security> selectByCondition(String sql) {
		DBContextHolder.setContextConnection(DatabaseDao.getConnection());
		return DBUTil.getResult(sql, Security.class);
	}

	public Security getSecurityById(String id) {
		String sql = "select * from security where id ='" + id + "'";
		DBContextHolder.setContextConnection(DatabaseDao.getConnection());
		List<Security> result = DBUTil.getResult(sql, Security.class);
		return result.size() == 0 ? null : result.get(0);
	}

	public List<Security> getAllRootSecurity() {
		String sql = "select * from security where parentid ='" + Constants.MODULE_ROOT + "'";
		DBContextHolder.setContextConnection(DatabaseDao.getConnection());
		List<Security> result = DBUTil.getResult(sql, Security.class);
		return result;
	}
}
